using System;
using System.Collections.Generic;
using System.Text;
using LeisureCenter.Api;
using LeisureCenter.Core;

namespace LeisureCenter.Playlist
{
    public class AudioCdPlaylistItem : PlaylistMediaItem
    {
        public AudioCdPlaylistItem(AudioCdPlaylist playlist)
            : base(playlist)
        {
            _media = ((AudioCdPlaylist)playlist).audioCdMedia;
        }

        protected override void CreateMedia()
        {
        }

        public override string Text
        {
            get { return ((IAudioCdMedia)this._media).GetTitle(this.Index); }
        }
    }

    public class AudioCdPlaylist : MediaPlaylist
    {
        internal IAudioCdMedia audioCdMedia;

        public AudioCdPlaylist(PlaylistManager playlistManager, string drive)
            : base(playlistManager)
        {
            audioCdMedia = (IAudioCdMedia)Program.LeisureCenterInstance.PlayersManager.AudioCdPlayer.CreateMedia(drive, MediaKind.CD);
            foreach (string title in audioCdMedia.Titles)
            {
                this.items.Add(new AudioCdPlaylistItem(this));
            }
            audioCdMedia.PlaylistChanged += new EventHandler(audioCdMedia_PlaylistChanged);
            audioCdMedia.MediaEnded += new EventHandler(audioCdMedia_MediaEnded);
        }

        void audioCdMedia_MediaEnded(object sender, EventArgs e)
        {
            HandleEndOfMedia();
        }

        void audioCdMedia_PlaylistChanged(object sender, EventArgs e)
        {
            this.playlistManager.OnPlaylistChanged();
        }

        protected internal override void playlistManager_PlayModeChanged(object sender, EventArgs e)
        {
        }

        private void RetrieveActivePlaylistItem()
        {
            int index = ActivePlaylistItemIndex;
            if (index >= 0 && index < items.Count)
                base.ActivePlaylistItem = this.items[index];
            else
                base.ActivePlaylistItem = null;
        }

        public override void Previous()
        {
            this.audioCdMedia.Previous();
            RetrieveActivePlaylistItem();
        }

        public override void Next()
        {
            this.audioCdMedia.Next();
            RetrieveActivePlaylistItem();
        }

        public override IPlaylistItem ActivePlaylistItem
        {
            get { return base.ActivePlaylistItem; }
            set
            {
                if (value != null)
                    ActivePlaylistItemIndex = value.Index;
                else
                {
                    audioCdMedia.Stop();
                    base.ActivePlaylistItem = null;
                }
            }
        }

        public override int ActivePlaylistItemIndex
        {
            get { return this.audioCdMedia.Track; }
            set
            {
                this.audioCdMedia.Track = value;
                RetrieveActivePlaylistItem();
            }
        }
    }
}
